﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Reflection;
using SMS.Core.Classes;

namespace SMS.Core
{
    /// <summary>
    /// 串口口配置类
    /// </summary>
    public class CommConfig
    {
        /// <summary>
        /// 当前目录物理路径
        /// </summary>
        public static string BasePath
        {
            get
            {
                return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            }
        }

        /// <summary>
        /// 平台配置文件路径
        /// </summary>
        public static string SMSCommConfig
        {
            get { return Path.Combine(BasePath, @"CommConfig.config"); }
        }

        /// <summary>
        /// 平台配置文件路径
        /// </summary>
        public static string SMSConsoleConfig
        {
            get { return Path.Combine(BasePath, @"SMSConsole.exe.config"); }
        }

        /// <summary>
        /// 档案配置文件路径
        /// </summary>
        public static string SMSServiceConfig
        {
            get { return Path.Combine(BasePath, @"SMSService.exe.Config"); }
        }

        /// <summary>
        /// 类型转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="text"></param>
        /// <returns></returns>
        public static T Convert<T>(string text)
        {
            if (string.IsNullOrEmpty(text)) return default(T);
            var convertType = typeof(T);

            if (convertType.BaseType.Name == "Enum")
            {
                return (T)Enum.Parse(convertType, text);
            }

            object objValue = null;
            var cMethod = string.Format("To{0}", convertType.Name);
            var mi = typeof(System.Convert).GetMethod(cMethod, new Type[] { typeof(string) });
            if (mi != null)
            {
                objValue = typeof(System.Convert).InvokeMember(
                    cMethod,
                    BindingFlags.InvokeMethod,
                    null,
                    null,
                    new object[] { text }
                );
            }
            else
            {
                return default(T);
            }
            return (T)objValue;
        }

        #region CommConfig

        /// <summary>
        /// 获取串口配置
        /// </summary>
        private static T GetSMSCommValue<T>(string attrKey)
        {
            var xmlSet = new XmlSetting(SMSCommConfig);
            var connNode = xmlSet.GetNodeWithAttribute("configuration/appSettings", "key", attrKey);
            return Convert<T>(xmlSet.GetNodeAttribute(connNode, "value"));
        }

        /// <summary>
        /// 串口号
        /// </summary>
        public static string PortName
        {
            get
            {
                var portName = GetSMSCommValue<string>("PortName");
                if(string.IsNullOrEmpty(portName)) portName = "COM1";
                return portName;
            }
        }

        /// <summary>
        /// 波特率
        /// </summary>
        public static int BaudRate
        {
            get
            {
                var boudRate = GetSMSCommValue<int>("BaudRate");
                if(boudRate == default(int)) boudRate = 9600;
                return boudRate;
            }
        }

        /// <summary>
        /// 校验位
        /// </summary>
        public static Parity Parity
        {
            get
            {
                var strParity = GetSMSCommValue<string>("Parity");
                var parity = Parity.None;
                switch (strParity)
                {
                    case "奇校验":
                        parity = Parity.Odd;
                        break;
                    case "偶校验":
                        parity = Parity.Even;
                        break;
                }
                return parity;
            }
        }

        /// <summary>
        /// 数据位
        /// </summary>
        public static int DataBits
        {
            get
            {
                var dataBits = GetSMSCommValue<int>("dataBits");
                if (dataBits == default(int)) dataBits = 8;
                return dataBits;
            }
        }

        /// <summary>
        /// 停止位
        /// </summary>
        public static StopBits StopBits
        {
            get
            {
                var strStopBits = GetSMSCommValue<string>("StopBits");
                var stopBits = StopBits.One;
                switch (strStopBits)
                {
                    case "1":
                        stopBits = StopBits.One;
                        break;
                    case "1.5":
                        stopBits = StopBits.OnePointFive;
                        break;
                    case "2":
                        stopBits = StopBits.Two;
                        break;
                }
                return stopBits;
            }
        }

        /// <summary>
        /// RTS电平触发方式
        /// </summary>
        public static bool RtsEnable
        {
            get
            {
                var rtsEnable = GetSMSCommValue<bool>("RtsEnable");
                //var rtsEnable = false;
                return rtsEnable;
            }
        }

        /// <summary>
        /// DTR电平触发方式
        /// </summary>
        public static bool DtrEnable
        {
            get
            {
                var dtrEnable = GetSMSCommValue<bool>("DtrEnable");
                //var dtrEnable = false;
                return dtrEnable;
            }
        }

        /// <summary>
        /// 重试次数
        /// </summary>
        public static int Retries
        {
            get
            {
                var retries = 1;
                return retries;
            }
        }

        /// <summary>
        /// 超时分钟
        /// </summary>
        public static int ExpireMimutes
        {
            get 
            {
                var expireMimute = 1440;
                return expireMimute;
            }
        }

        #endregion

        #region ConsoleConfig

        /// <summary>
        /// 客户端服务地址
        /// </summary>
        public static string ClientAddress
        {
            get
            {
                var xmlSet = new XmlSetting(SMSConsoleConfig);
                var connNode = xmlSet.GetFirstNode("configuration/system.serviceModel/client");
                return xmlSet.GetNodeAttribute(connNode, "address");
            }
        }

        #endregion

        #region ServiceConfig

        /// <summary>
        /// 服务地址
        /// </summary>
        public static string ServiceAddress
        {
            get
            {
                var xmlSet = new XmlSetting(SMSServiceConfig);
                var connNode = xmlSet.GetFirstNode("configuration/system.serviceModel/services/service/host/baseAddresses");
                return xmlSet.GetNodeAttribute(connNode, "baseAddress");
            }
        }

        /// <summary>
        /// 数据库连接串
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                var xmlSet = new XmlSetting(SMSServiceConfig);
                var connNode = xmlSet.GetFirstNode("configuration/appSettings");
                return xmlSet.GetNodeAttribute(connNode, "value");
            }
        }

        #endregion
    }
}
